import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/cart_controller.dart';
import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/controller/restaurant_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/controller/user_controller.dart';
import 'package:efood_multivendor/data/model/response/cart_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/data/model/response/restaurant_model.dart';
import 'package:efood_multivendor/helper/price_converter.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/age_dialog.dart';
import 'package:efood_multivendor/view/base/cart_view.dart';
import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/base/not_available_widget.dart';
import 'package:efood_multivendor/view/base/not_logged_in_screen.dart';
import 'package:efood_multivendor/view/base/quantity_button.dart';
import 'package:efood_multivendor/view/screens/home/home_screen.dart';
import 'package:efood_multivendor/view/screens/restaurant/restaurant_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

const _cardColor = LinearGradient(colors: [
  Color(0xffff2222),
  Color(0xffff8022),
]);
const _cardColor2 = LinearGradient(colors: [
  Color.fromARGB(255, 201, 201, 201),
  Color.fromARGB(255, 194, 194, 194),
]);
const _maxHeight = 350.0;
const _minHeight = 62.0;

class ProductScreen extends StatefulWidget {
  final Product product;
  final bool isCampaign;
  final CartModel? cart;
  final int? cartIndex;
  final bool inRestaurantPage;
  final String? price;
  final bool fromCategory;
  final bool fromSuggestion;

  ProductScreen(
      {Key? key,
      required this.product,
      this.isCampaign = false,
      this.cart,
      this.cartIndex,
      this.inRestaurantPage = false,
      this.fromCategory = false,
      this.fromSuggestion = false,
      this.price})
      : super(key: key);

  @override
  State<ProductScreen> createState() => _ProductScreenState();
}

class _ProductScreenState extends State<ProductScreen>
    with SingleTickerProviderStateMixin {
  final ScrollController scrollController = ScrollController();

  Restaurant? restaurant;
  CartModel? _cartModel;
  static const int YearInHour = 8760;
  late AnimationController _controller;
  late Animation<double> _animation;
  @override
  void initState() {
    super.initState();
    Get.find<RestaurantController>().getRestaurantDetails(
        Restaurant(id: widget.product.restaurantId), 'product');
    Get.find<ProductController>().initData(widget.product, widget.cart);
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 1),
    );
    _animation = Tween<double>(
      begin: 0,
      end: 1,
    ).animate(_controller);

    _controller.repeat(reverse: true);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return GetBuilder<CartController>(builder: (cartController) {
      return Scaffold(
          backgroundColor: Theme.of(context).cardColor,
          body: GestureDetector(
            onHorizontalDragUpdate: (details) {
              int sensitivity = 12;
              if (details.delta.dx > sensitivity) {
                Get.back();
              } else if (details.delta.dx < -sensitivity) {}
            },
            child: AnnotatedRegion<SystemUiOverlayStyle>(
                value: SystemUiOverlayStyle.light,
                child: CartView(
                    Stack(
                      children: [
                        GetBuilder<RestaurantController>(
                            builder: (restController) {
                          return restController.restaurant != null
                              ? GetBuilder<ProductController>(
                                  builder: (productController) {
                                  List<bool> isThere = [];
                                  if (widget.product.addOns?.isNotEmpty ==
                                      true) {
                                    if (productController.addOnActiveList[
                                            widget.product.id] !=
                                        null) {
                                      if (cartController.cartList.isEmpty &&
                                          productController.addOnActiveList[
                                                      widget.product.id]
                                                  ?.contains(true) ==
                                              true) {
                                        for (int i = 0;
                                            i <
                                                productController
                                                    .addOnActiveList[
                                                        widget.product.id]!
                                                    .length;
                                            i++) {
                                          productController.addOnActiveList[
                                              widget.product.id]![i] = false;
                                          productController.addOnQtyList[i] = 1;
                                        }
                                      }
                                    }

                                    if (cartController.cartList.isNotEmpty) {
                                      for (int i = 0;
                                          i < cartController.cartList.length;
                                          i++) {
                                        if (cartController
                                                .cartList[i].product!.id ==
                                            widget.product.id) {
                                          isThere.add(true);
                                        } else {
                                          isThere.add(false);
                                        }
                                      }
                                      if (isThere.contains(true)) {
                                      } else {
                                        for (int i = 0;
                                            i <
                                                productController
                                                    .addOnActiveList.length;
                                            i++) {
                                          productController.addOnActiveList[
                                              widget.product.id]![i] = false;
                                          productController.addOnQtyList[i] = 1;
                                        }
                                      }
                                    }
                                  }

                                  bool tooFar = true;
                                  bool isOpen = false;
                                  int status = 0;
                                  String offDay = 'sth';
                                  bool active = false;
                                  if (restaurant != null) {
                                    tooFar =
                                        restController.restaurant?.too_far ??
                                            true;
                                    isOpen =
                                        restController.restaurant?.is_open ??
                                            false;
                                    status =
                                        restController.restaurant?.status ?? 0;
                                    offDay =
                                        restController.restaurant?.offDay ??
                                            'sth';

                                    if (restaurant?.status == 1 &&
                                        !tooFar &&
                                        isOpen &&
                                        offDay.isEmpty) {
                                      active = true;
                                    }
                                  }

                                  double? _startingPrice;
                                  double? _endingPrice;
                                  if (widget
                                          .product.choiceOptions?.isNotEmpty ==
                                      true) {
                                    List<double> _priceList = [];
                                    widget.product.variations?.forEach(
                                        (variation) =>
                                            _priceList.add(variation.price!));
                                    _priceList.sort((a, b) => a.compareTo(b));
                                    _startingPrice = _priceList[0];
                                    if (_priceList[0] <
                                        _priceList[_priceList.length - 1]) {
                                      _endingPrice =
                                          _priceList[_priceList.length - 1];
                                    }
                                  } else {
                                    _startingPrice = widget.product.price;
                                  }
                                  List<String> _variationList = [];
                                  for (int index = 0;
                                      index <
                                          (widget.product.choiceOptions
                                                  ?.length ??
                                              0);
                                      index++) {
                                    _variationList.add(widget
                                        .product
                                        .choiceOptions![index]
                                        .options![productController
                                            .variationIndex![index]]
                                        .replaceAll(' ', ''));
                                  }
                                  String variationType = '';
                                  bool isFirst = true;
                                  _variationList.forEach((variation) {
                                    if (isFirst) {
                                      variationType =
                                          '$variationType$variation';
                                      isFirst = false;
                                    } else {
                                      variationType =
                                          '$variationType-$variation';
                                    }
                                  });

                                  double? price = widget.product.price;
                                  Variation? _variation;
                                  double _discount = 0;
                                  String _discountType;
                                  double _productDiscount = 0;
                                  double _restaurantDiscount = 0;
                                  for (Variation variation
                                      in widget.product.variations ?? []) {
                                    if (variation.type == variationType) {
                                      price = variation.price;
                                      _variation = variation;
                                      break;
                                    }
                                  }
                                  // _productDiscount =
                                  //     widget.product.discountType == 'percent'
                                  //         ? (_discount / 100) *
                                  //             widget.product.price
                                  //         : widget.product.discount;
                                  // _restaurantDiscount = Get.find<
                                  //                     RestaurantController>()
                                  //                 .restaurant !=
                                  //             null &&
                                  //         Get.find<RestaurantController>()
                                  //                 .restaurant
                                  //                 .discount !=
                                  //             null
                                  //     ? Get.find<RestaurantController>()
                                  //                 .restaurant
                                  //                 .discount
                                  //                 .discountType ==
                                  //             'percent'
                                  //         ? (Get.find<RestaurantController>()
                                  //                     .restaurant
                                  //                     .discount
                                  //                     .discount /
                                  //                 100) *
                                  //             widget.product.price
                                  //         : Get.find<RestaurantController>()
                                  //             .restaurant
                                  //             .discount
                                  //             .discount
                                  //     : 0;

                                  _discountType = 'amount';
                                  _discount =
                                      widget.product.discountedItemPrice ?? 0;

                                  double priceWithDiscount =
                                      PriceConverter.convertWithDiscount(
                                          price ?? 0, _discount, _discountType);
                                  double priceWithQuantity = priceWithDiscount *
                                      productController.quantity;
                                  double? addonsCost;
                                  List<AddOn> _addonlist = [];
                                  List<AddOns> _addonslist = [];

                                  cartController.cartList.forEach((cartModel) {
                                    for (int i = 0;
                                        i < (cartModel.addOnIds?.length ?? 0);
                                        i++) {
                                      if (_addonlist
                                          .contains(cartModel.addOnIds![i])) {
                                      } else {
                                        _addonlist.add(cartModel.addOnIds![i]);
                                      }
                                    }
                                  });

                                  bool _isRestAvailable =
                                      widget.product.is_open == true &&
                                          widget.product.restaurantTooFar !=
                                              true;
                                  bool _isFoodAvailable =
                                      widget.product.is_open == true;

                                  bool _isAvailable =
                                      _isRestAvailable && _isFoodAvailable;

                                  _cartModel = CartModel(
                                      price,
                                      _discount,
                                      _variation != null ? [_variation] : [],
                                      price! - _discount,
                                      productController.quantity,
                                      [],
                                      [],
                                      widget.isCampaign,
                                      widget.product,
                                      addonsCost);
                                  Product product = widget.product;
                                  bool _hasVars =
                                      (product.variations?.isNotEmpty == true);

                                  bool _hasAddons =
                                      product.addOns?.isNotEmpty == true;
                                  final LinearGradient linearGradient =
                                      LinearGradient(
                                    colors: <Color>[
                                      Color(0xffff8022),
                                      Color(0xffff2222)
                                    ],
                                  );
                                  return SingleChildScrollView(
                                    child: SizedBox(
                                      height: 100.h,
                                      child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Stack(
                                              clipBehavior: Clip.none,
                                              children: [
                                                ClipRRect(
                                                    borderRadius:
                                                        BorderRadius.vertical(
                                                            bottom:
                                                                Radius.circular(
                                                                    33)),
                                                    child: CustomImage(
                                                      image:
                                                          '${widget.product.image}',
                                                      height: ResponsiveHelper
                                                              .isMobile(context)
                                                          ? 35.h
                                                          : 140,
                                                      width: ResponsiveHelper
                                                              .isMobile(context)
                                                          ? 100.w
                                                          : 200,
                                                      fit: BoxFit.cover,
                                                    )),
                                                _isAvailable
                                                    ? SizedBox()
                                                    : NotAvailableWidget(
                                                        isRestaurant: false,
                                                        fontSize: Dimensions
                                                            .fontSizeOverLarge,
                                                      ),
                                                Positioned(
                                                  bottom: -5.h,
                                                  left: 5.w,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.bottomCenter,
                                                    child: Container(
                                                        decoration: BoxDecoration(
                                                            boxShadow: [
                                                              BoxShadow(
                                                                color: Theme.of(
                                                                        context)
                                                                    .primaryColor
                                                                    .withOpacity(
                                                                        0.05),
                                                                spreadRadius: 3,
                                                                blurRadius: 5,
                                                                offset: Offset(
                                                                    0, 3),
                                                              ),
                                                            ],
                                                            color: Theme.of(
                                                                    context)
                                                                .cardColor,
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        3.h)),
                                                        width: 90.w,
                                                        height: 10.h,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .all(8.0),
                                                          child: Row(
                                                            crossAxisAlignment:
                                                                CrossAxisAlignment
                                                                    .center,
                                                            children: [
                                                              SizedBox(
                                                                width: 60.w,
                                                                child: Text(
                                                                  product.name ??
                                                                      '',
                                                                  style: sfRegular
                                                                      .copyWith(
                                                                          fontSize:
                                                                              5.w),
                                                                  maxLines: 2,
                                                                  overflow:
                                                                      TextOverflow
                                                                          .ellipsis,
                                                                ),
                                                              ),
                                                              _cartModel?.price ==
                                                                      _cartModel
                                                                          ?.discountedPrice
                                                                  ? Text(
                                                                      PriceConverter.convertPrice(
                                                                          product.price ??
                                                                              0,
                                                                          asFixed:
                                                                              0),
                                                                      style: sfBlack
                                                                          .copyWith(
                                                                        color: Theme.of(context)
                                                                            .colorScheme
                                                                            .secondary,
                                                                        fontSize:
                                                                            6.w,
                                                                      ),
                                                                    )
                                                                  : Column(
                                                                      children: [
                                                                        Text(
                                                                          PriceConverter.convertPrice(
                                                                              _cartModel?.discountedPrice ?? 0,
                                                                              asFixed: 0),
                                                                          style:
                                                                              sfBlack.copyWith(
                                                                            color:
                                                                                Theme.of(context).colorScheme.secondary,
                                                                            fontSize:
                                                                                7.w,
                                                                          ),
                                                                        ),
                                                                        SizedBox(
                                                                          width:
                                                                              2.w,
                                                                        ),
                                                                        Text(
                                                                          PriceConverter.convertPrice(
                                                                              _cartModel?.price ?? 0,
                                                                              asFixed: 0),
                                                                          style:
                                                                              sfMedium.copyWith(
                                                                            fontSize:
                                                                                5.w,
                                                                            color:
                                                                                Theme.of(context).disabledColor,
                                                                            decoration:
                                                                                TextDecoration.lineThrough,
                                                                            decorationColor:
                                                                                Theme.of(context).disabledColor,
                                                                          ),
                                                                        )
                                                                      ],
                                                                    ),
                                                            ],
                                                          ),
                                                        )),
                                                  ),
                                                ),
                                                ClipRRect(
                                                  borderRadius:
                                                      BorderRadius.circular(10),
                                                  clipBehavior: Clip.antiAlias,
                                                  child: Container(
                                                    height: 35.h,
                                                    decoration: BoxDecoration(
                                                      gradient: LinearGradient(
                                                        begin:
                                                            Alignment.topCenter,
                                                        end: Alignment
                                                            .bottomCenter,
                                                        colors: [
                                                          Colors.black
                                                              .withOpacity(0.5),
                                                          Colors.transparent,
                                                        ],
                                                      ),
                                                    ),
                                                  ),
                                                ),
                                                Positioned(
                                                  top: 4.h,
                                                  left: 2.w,
                                                  child: InkWell(
                                                    onTap: () {
                                                      if (!cartController
                                                          .isCartOpen) {
                                                        if (Get.find<SplashController>().showType != null &&
                                                            Get.find<SplashController>()
                                                                    .showType !=
                                                                null &&
                                                            Get.find<SplashController>()
                                                                    .showType
                                                                    ?.isNotEmpty ==
                                                                true) {
                                                          Get.find<
                                                                  SplashController>()
                                                              .setNotificationPayload(
                                                                  "", "");
                                                          Get.find<
                                                                  SplashController>()
                                                              .loadData(false);
                                                          Get.offNamed(RouteHelper
                                                              .getInitialRoute(
                                                                  "saved address found"));
                                                        } else {
                                                          Get.back();
                                                        }
                                                      } else {
                                                        cartController
                                                            .closeCart();
                                                      }
                                                    },
                                                    child: Container(
                                                        width: 4.h,
                                                        height: 4.h,
                                                        padding:
                                                            EdgeInsets.only(
                                                                right: 2.h),
                                                        decoration:
                                                            BoxDecoration(
                                                                shape: BoxShape
                                                                    .circle,
                                                                color: Colors
                                                                    .white
                                                                    .withOpacity(
                                                                        0.7)),
                                                        child: Center(
                                                          child: Icon(
                                                            Icons.chevron_left,
                                                            color: Colors.black,
                                                            size: 4.h,
                                                          ),
                                                        )),
                                                  ),
                                                ),
                                              ],
                                            ),
                                            SizedBox(height: 5.h),
                                            SizedBox(
                                              height: 2.h,
                                            ),
                                            Padding(
                                              padding: const EdgeInsets.only(
                                                  left: 8.0),
                                              child: TextButton(
                                                  onPressed: () async {
                                                    if (restController
                                                                .restaurant
                                                                ?.is_open ==
                                                            true &&
                                                        restController
                                                                .restaurant
                                                                ?.status ==
                                                            1 &&
                                                        restController
                                                                .restaurant
                                                                ?.offDay
                                                                ?.isNotEmpty !=
                                                            true &&
                                                        restController
                                                                .restaurant
                                                                ?.too_far !=
                                                            true) {
                                                      Get.toNamed(
                                                          RouteHelper
                                                              .getRestaurantRoute(
                                                                  product
                                                                      .restaurantId!),
                                                          arguments: RestaurantScreen(
                                                              restaurant:
                                                                  restController
                                                                      .restaurant!));
                                                    } else {
                                                      showCustomSnackBar(
                                                          'restaurant_is_not_available'
                                                              .tr);
                                                    }
                                                  },
                                                  child: Text(
                                                      '${restController.restaurant?.name}')),
                                            ),
                                            Padding(
                                              padding: EdgeInsets.symmetric(
                                                horizontal: 5.w,
                                              ),
                                              child: Text(
                                                "description".tr,
                                                style: sfRegular.copyWith(
                                                    fontSize: 5.w,
                                                    fontWeight:
                                                        FontWeight.w500),
                                              ),
                                            ),
                                            SizedBox(
                                              height: 1.5.h,
                                            ),
                                            Padding(
                                                padding: EdgeInsets.symmetric(
                                                  horizontal: 5.w,
                                                ),
                                                child: Text(
                                                  product.description ?? " ",
                                                  style: sfRegular,
                                                  maxLines: 3,
                                                )),
                                            SizedBox(
                                              height: 1.9.h,
                                            ),
                                            widget.product.addOns?.isNotEmpty ==
                                                    true
                                                ? Padding(
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                            horizontal: 5.w),
                                                    child: Column(
                                                        crossAxisAlignment:
                                                            CrossAxisAlignment
                                                                .start,
                                                        children: [
                                                          AnimatedBuilder(
                                                            animation:
                                                                _animation,
                                                            builder: (context,
                                                                child) {
                                                              return Opacity(
                                                                opacity:
                                                                    _animation
                                                                        .value,
                                                                child: Text(
                                                                  'customize_order'
                                                                      .tr
                                                                      .tr,
                                                                  style: sfRegular.copyWith(
                                                                      color: Theme.of(
                                                                              context)
                                                                          .primaryColor,
                                                                      fontSize:
                                                                          5.w,
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w500),
                                                                ),
                                                              );
                                                            },
                                                          ),
                                                          Container(
                                                            height: 85,
                                                            child: Column(
                                                              children: [
                                                                Expanded(
                                                                  child: ListView
                                                                      .builder(
                                                                    scrollDirection:
                                                                        Axis.horizontal,
                                                                    shrinkWrap:
                                                                        true,
                                                                    physics:
                                                                        BouncingScrollPhysics(),
                                                                    itemCount: widget
                                                                            .product
                                                                            .addOns
                                                                            ?.length ??
                                                                        0,
                                                                    itemBuilder:
                                                                        (context,
                                                                            index) {
                                                                      return InkWell(
                                                                        onTap:
                                                                            () {
                                                                          if (cartController
                                                                              .cartList
                                                                              .isNotEmpty) {
                                                                            if (productController.addOnActiveList[product.id]?[index] !=
                                                                                true) {
                                                                              cartController.cartRest[widget.product.restaurantId]?.forEach((cartModel) {
                                                                                if (cartModel.product?.id == widget.product.id) {
                                                                                  productController.addAddOn(true, index, product.id!);

                                                                                  AddOn addon = AddOn(id: widget.product.addOns?[index].id, quantity: productController.addOnQtyList[index], price: widget.product.addOns![index].price);

                                                                                  if (widget.product.restaurantId != null) {
                                                                                    if (cartController.cartRest.isNotEmpty) {
                                                                                      if (cartModel.product?.id == widget.product.id) {
                                                                                        for (AddOns addOns in cartModel.product?.addOns ?? []) {
                                                                                          if (addOns.id == addon.id) {
                                                                                            cartModel.addOnIds?.add(addon);
                                                                                          }
                                                                                        }
                                                                                      }
                                                                                    }
                                                                                  } else {
                                                                                    cartController.cartList.forEach((cartModel) {});
                                                                                  }
                                                                                }
                                                                              });

                                                                              cartController.addAddon();
                                                                            } else if (productController.addOnQtyList[index] <=
                                                                                1) {
                                                                              cartController.cartRest[widget.product.restaurantId]?.forEach((cartmodel) {
                                                                                if (cartmodel.product?.id == widget.product.id) {
                                                                                  if (cartmodel.product?.id == widget.product.id) {
                                                                                    productController.addAddOn(false, index, product.id!);

                                                                                    if (widget.product.restaurantId != null) {
                                                                                      if (cartController.cartRest.isNotEmpty) {
                                                                                        if (cartController.cartRest.containsKey(widget.product.restaurantId)) {
                                                                                          if (cartmodel.product?.id == widget.product.id) {
                                                                                            for (int i = 0; i < (cartmodel.addOnIds?.length ?? 0); i++) {
                                                                                              if (cartmodel.addOnIds![i].id == widget.product.addOns![index].id) {
                                                                                                if (cartmodel.addOnIds![i].quantity == 1) {
                                                                                                  cartmodel.addOnIds!.removeWhere((element) => element.id == widget.product.addOns![index].id);
                                                                                                  cartController.totalAddonPrice(widget.product.addOns?[index].price ?? 0, widget.product.addOns![index].id!, widget.product.id!, false, false);
                                                                                                  break;
                                                                                                }
                                                                                              }
                                                                                            }
                                                                                          }
                                                                                        }
                                                                                      }
                                                                                    } else {
                                                                                      cartController.cartList.forEach((cartModel) {});
                                                                                    }
                                                                                  }
                                                                                }
                                                                              });
                                                                              cartController.removeAddOn(
                                                                                index,
                                                                                widget.product.addOns?[index].price ?? 0,
                                                                              );
                                                                            }
                                                                          }
                                                                        },
                                                                        child:
                                                                            Container(
                                                                          width:
                                                                              80,
                                                                          height:
                                                                              70,
                                                                          padding:
                                                                              EdgeInsets.all(Dimensions.PADDING_SIZE_EXTRA_SMALL),
                                                                          alignment:
                                                                              Alignment.center,
                                                                          margin: EdgeInsets.only(
                                                                              right: Dimensions.PADDING_SIZE_DEFAULT,
                                                                              top: Dimensions.PADDING_SIZE_DEFAULT),
                                                                          decoration:
                                                                              BoxDecoration(
                                                                            color: productController.addOnActiveList[product.id]?[index] == true
                                                                                ? Theme.of(context).primaryColor
                                                                                : Theme.of(context).colorScheme.surface,
                                                                            borderRadius:
                                                                                BorderRadius.circular(Dimensions.RADIUS_SMALL),
                                                                            border: productController.addOnActiveList[product.id]?[index] == true
                                                                                ? null
                                                                                : Border.all(color: Theme.of(context).disabledColor, width: 2),
                                                                            boxShadow: productController.addOnActiveList[product.id]?[index] == true
                                                                                ? [
                                                                                    BoxShadow(color: Colors.grey[Get.isDarkMode ? 700 : 300]!, blurRadius: 5, spreadRadius: 1)
                                                                                  ]
                                                                                : null,
                                                                          ),
                                                                          child:
                                                                              Column(children: [
                                                                            Expanded(
                                                                              child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
                                                                                Text(
                                                                                  widget.product.addOns?[index].name ?? '',
                                                                                  maxLines: 2,
                                                                                  overflow: TextOverflow.ellipsis,
                                                                                  textAlign: TextAlign.center,
                                                                                  style: sfMedium.copyWith(
                                                                                    color: productController.addOnActiveList[product.id]?[index] == true ? Colors.white : Colors.black,
                                                                                    fontSize: Dimensions.fontSizeSmall,
                                                                                  ),
                                                                                ),
                                                                                SizedBox(height: 5),
                                                                                Text(
                                                                                  (widget.product.addOns?[index].price ?? 0) > 0 ? PriceConverter.convertPrice(widget.product.addOns![index].price! * productController.addOnQtyList[index]) : 'free'.tr,
                                                                                  maxLines: 1,
                                                                                  overflow: TextOverflow.ellipsis,
                                                                                  style: sfRegular.copyWith(
                                                                                    color: productController.addOnActiveList[product.id]?[index] == true ? Colors.white : Colors.black,
                                                                                    fontSize: Dimensions.fontSizeExtraSmall,
                                                                                  ),
                                                                                ),
                                                                              ]),
                                                                            ),
                                                                            productController.addOnActiveList[product.id]?[index] == true
                                                                                ? Container(
                                                                                    height: 25,
                                                                                    decoration: BoxDecoration(borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL), color: Theme.of(context).cardColor),
                                                                                    child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                                                                                      Expanded(
                                                                                        child: InkWell(
                                                                                          onTap: () {
                                                                                            if (cartController.cartList.isNotEmpty) {
                                                                                              if (productController.addOnQtyList[index] > 1) {
                                                                                                cartController.cartRest[widget.product.restaurantId]?.forEach((cartmodel) {
                                                                                                  if (cartmodel.product?.id == widget.product.id) {
                                                                                                    productController.setAddOnQuantity(false, index);
                                                                                                    cartController.removeAddOn(
                                                                                                      index,
                                                                                                      widget.product.addOns![index].price!,
                                                                                                    );
                                                                                                    if (widget.product.restaurantId != null) {
                                                                                                      if (cartController.cartRest.isNotEmpty) {
                                                                                                        if (cartController.cartRest.containsKey(widget.product.restaurantId)) {
                                                                                                          if (cartmodel.product?.id == widget.product.id) {
                                                                                                            for (int i = 0; i < (cartmodel.addOnIds?.length ?? 0); i++) {
                                                                                                              if (cartmodel.addOnIds?[i].id == widget.product.addOns?[index].id) {
                                                                                                                int price = 1;
                                                                                                                if (cartmodel.addOnIds![i].quantity! <= 1) {
                                                                                                                  cartmodel.addOnIds!.removeWhere((element) => element.id == widget.product.addOns?[index].id);

                                                                                                                  break;
                                                                                                                } else {
                                                                                                                  cartmodel.addOnIds?[i].decrementAddonQuantity = price;
                                                                                                                  break;
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    } else {
                                                                                                      cartController.cartList.forEach((cartModel) {});
                                                                                                    }
                                                                                                  }
                                                                                                });
                                                                                              } else {
                                                                                                cartController.cartList.forEach((cartmodel) {
                                                                                                  if (cartmodel.product?.id == widget.product.id) {
                                                                                                    if (cartmodel.product?.id == widget.product.id) {
                                                                                                      productController.addAddOn(false, index, product.id!);
                                                                                                      cartController.removeAddOn(
                                                                                                        index,
                                                                                                        widget.product.addOns?[index].price ?? 0,
                                                                                                      );
                                                                                                      if (widget.product.restaurantId != null) {
                                                                                                        if (cartController.cartRest.isNotEmpty) {
                                                                                                          if (cartController.cartRest.containsKey(widget.product.restaurantId)) {
                                                                                                            cartController.cartRest[widget.product.restaurantId]?.forEach((cartModel) {
                                                                                                              if (cartmodel.product?.id == widget.product.id) {
                                                                                                                for (int i = 0; i < (cartmodel.addOnIds?.length ?? 0); i++) {
                                                                                                                  if (cartmodel.addOnIds?[i].id == widget.product.addOns?[index].id) {
                                                                                                                    int price = 1;
                                                                                                                    if ((cartmodel.addOnIds?[i].quantity ?? 0) <= 1) {
                                                                                                                      cartmodel.addOnIds?.removeWhere((element) => element.id == widget.product.addOns?[index].id);
                                                                                                                      cartController.totalAddonPrice(widget.product.addOns?[index].price ?? 0, widget.product.addOns![index].id!, widget.product.id!, false, false);
                                                                                                                      break;
                                                                                                                    } else {
                                                                                                                      cartmodel.addOnIds?[i].decrementAddonQuantity = price;
                                                                                                                      break;
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            });
                                                                                                          }
                                                                                                        }
                                                                                                      } else {
                                                                                                        cartController.cartList.forEach((cartModel) {});
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                });
                                                                                              }
                                                                                            }
                                                                                          },
                                                                                          child: Center(child: Icon(Icons.remove, size: 15)),
                                                                                        ),
                                                                                      ),
                                                                                      Text(
                                                                                        productController.addOnQtyList[index].toString(),
                                                                                        style: sfMedium.copyWith(fontSize: Dimensions.fontSizeSmall),
                                                                                      ),
                                                                                      Expanded(
                                                                                        child: InkWell(
                                                                                          onTap: () {
                                                                                            if (cartController.cartList.isNotEmpty) {
                                                                                              cartController.cartList.forEach((cartmodel) {
                                                                                                if (cartmodel.product?.id == widget.product.id) {
                                                                                                  productController.setAddOnQuantity(true, index);
                                                                                                  cartController.addAddon();
                                                                                                  if (widget.product.restaurantId != null) {
                                                                                                    if (cartController.cartRest.isNotEmpty) {
                                                                                                      if (cartController.cartRest.containsKey(widget.product.restaurantId)) {
                                                                                                        cartController.cartRest[widget.product.restaurantId]?.forEach((cartModel) {
                                                                                                          if (cartmodel.product?.id == widget.product.id) {
                                                                                                            for (int i = 0; i < (cartmodel.addOnIds?.length ?? 0); i++) {
                                                                                                              if (cartmodel.addOnIds?[i].id == widget.product.addOns?[index].id) {
                                                                                                                int price = 1;

                                                                                                                cartmodel.addOnIds?[i].incrementAddonQuantity = price;

                                                                                                                break;
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        });
                                                                                                      }
                                                                                                    }
                                                                                                  } else {
                                                                                                    cartController.cartList.forEach((cartModel) {});
                                                                                                  }
                                                                                                }
                                                                                              });
                                                                                            }
                                                                                          },
                                                                                          child: Center(child: Icon(Icons.add, size: 15)),
                                                                                        ),
                                                                                      ),
                                                                                    ]),
                                                                                  )
                                                                                : SizedBox(),
                                                                          ]),
                                                                        ),
                                                                      );
                                                                    },
                                                                  ),
                                                                ),
                                                              ],
                                                            ),
                                                          ),
                                                          SizedBox(
                                                              height: Dimensions
                                                                  .PADDING_SIZE_EXTRA_SMALL),
                                                        ]),
                                                  )
                                                : SizedBox(),
                                            Padding(
                                              padding: EdgeInsets.symmetric(
                                                  horizontal: 5.w,
                                                  vertical: 3.h),
                                              child: Row(children: [
                                                Expanded(child: SizedBox()),
                                                GetBuilder<CartController>(
                                                    builder: (cartController) {
                                                  return Row(children: [
                                                    QuantityButton(
                                                      isActive: _isAvailable,
                                                      onTap: _isAvailable
                                                          ? () {
                                                              if (cartController
                                                                      .cartRest
                                                                      .length ==
                                                                  1) {
                                                                cartController
                                                                    .totalAddonPrice(
                                                                        0,
                                                                        0,
                                                                        0,
                                                                        false,
                                                                        true);
                                                              }
                                                              cartController
                                                                  .removeFromCart(
                                                                      _cartModel!,
                                                                      true);
                                                            }
                                                          : () {},
                                                      isIncrement: false,
                                                    ),
                                                    SizedBox(
                                                      width: 2.w,
                                                    ),
                                                    Text(
                                                        cartController
                                                            .getQuantity(
                                                                product)
                                                            .toString(),
                                                        style: sfMedium.copyWith(
                                                            fontSize: Dimensions
                                                                .fontSizeExtraLarge)),
                                                    SizedBox(
                                                      width: 2.w,
                                                    ),
                                                    QuantityButton(
                                                      isActive: _isAvailable,
                                                      onTap: () {
                                                        if (!_isAvailable) {
                                                          return;
                                                        }
                                                        if (Get.find<
                                                                    AuthController>()
                                                                .isLoggedIn() &&
                                                            product.is_alcohol ==
                                                                true) {
                                                          if (Get.find<
                                                                      UserController>()
                                                                  .userInfoModel
                                                                  ?.birthDate
                                                                  ?.isNotEmpty ==
                                                              true) {
                                                            DateTime birthday =
                                                                DateTime.parse(Get
                                                                        .find<
                                                                            UserController>()
                                                                    .userInfoModel!
                                                                    .birthDate!);
                                                            if ((DateTime.now()
                                                                        .difference(
                                                                            birthday)
                                                                        .inHours /
                                                                    YearInHour) >=
                                                                21) {
                                                              callback();
                                                            } else {
                                                              Get.dialog(
                                                                  AgeDialog(
                                                                callback:
                                                                    callback,
                                                              ));
                                                            }
                                                          } else {
                                                            Get.dialog(
                                                                AgeDialog(
                                                              callback:
                                                                  callback,
                                                            ));
                                                          }
                                                        } else if (!Get.find<
                                                                    AuthController>()
                                                                .isLoggedIn() &&
                                                            product.is_alcohol ==
                                                                true) {
                                                          Get.dialog(Scaffold(
                                                              body:
                                                                  NotLoggedInScreen()));
                                                        } else {
                                                          callback();
                                                        }
                                                      },
                                                      isIncrement: true,
                                                    ),
                                                  ]);
                                                }),
                                              ]),
                                            ),
                                            SizedBox(
                                              height: 12.h,
                                            )
                                          ]),
                                    ),
                                  );
                                })
                              : Center(child: CircularProgressIndicator());
                        })
                      ],
                    ),
                    "",
                    restaurantId: widget.product.restaurantId)),
          ),
          extendBody: true,
          bottomNavigationBar: cartController.cartview);
    });
  }

  void callback() {
    CartController cartController = Get.find<CartController>();
    if (widget.fromSuggestion) {
      cartController.setCurrentSuggestion(_cartModel!.product!.restaurantId!);
      if (cartController.previousSuggestion != 0 &&
          cartController.previousSuggestion !=
              cartController.currentSuggestion) {
        cartController
            .clearPreviousRestaurant(cartController.previousSuggestion);
      }
      cartController.addToCart(_cartModel!);
      cartController.setPreviousSuggestion(_cartModel!.product!.restaurantId!);
    } else if (widget.fromCategory) {
      cartController.setCurrentCategory(_cartModel!.product!.restaurantId!);
      if (cartController.previousCategory != 0 &&
          cartController.previousCategory != cartController.currentCategory) {
        cartController.clearPreviousRestaurant(cartController.previousCategory);
      }
      cartController.addToCart(_cartModel!);
      cartController.setPreviousCategory(_cartModel!.product!.restaurantId!);
    } else {
      cartController.addToCart(_cartModel!);
    }
  }
}
